package org.example.zerenlian.chain.service;

import jakarta.annotation.Resource;
import org.example.zerenlian.chain.enums.ChainEnums;
import org.example.zerenlian.chain.intf.ChainInterface;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author czj
 * @date 2024/12/7
 * @description
 */
@Service
public class ChainService {


    @Lazy
    @Resource()
    private Map<ChainEnums, List<ChainInterface>> initChainMap;

    @Bean
    public Map<ChainEnums, List<ChainInterface>> initChainMap(List<ChainInterface> chainInterfaces) {
       return chainInterfaces.stream().collect(Collectors.groupingBy(ChainInterface::type));
    }


    public void process(ChainEnums chain, Object param) {
        List<ChainInterface> chainInterfaces = initChainMap.get(chain);
        for (ChainInterface chainInterface : chainInterfaces) {
            chainInterface.process(param);
        }
    }
}
